package study.domain.blog;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.Size;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import org.hibernate.annotations.FetchMode;
import org.hibernate.validator.constraints.NotEmpty;

import study.domain.user.User;

import com.barney.support.domain.HasCreatedDate;
import com.barney.support.jpa.CreatedDateListener;

@Entity(name = "comments")
@Data
@EqualsAndHashCode(of = { "id" })
@ToString(exclude = "post")
@EntityListeners(CreatedDateListener.class)
public class Comment implements Serializable, HasCreatedDate {

	private static final long serialVersionUID = 7364762862911391053L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column
	private Long id;

	@OneToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "user_id", nullable = false, updatable = false)
	@org.hibernate.annotations.ForeignKey(name = "fk_comments_user_id")
	private User user;

	@ManyToOne(fetch = FetchType.LAZY)
	@org.hibernate.annotations.Fetch(FetchMode.JOIN)
	@JoinColumn(name = "post_id", nullable = false, updatable = false)
	@org.hibernate.annotations.ForeignKey(name = "fk_comments_post_id")
	private Post post;

	@NotEmpty
	@Size(min = 2, max = 500)
	@Column(nullable = false, length = 500)
	private String contents;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable = false, updatable = false)
	private Date createdDate;
}
